package com.jhc.config;

import com.jhc.utils.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ValidationException;

/**
 * @Author: zfm
 * @Date: 2019/11/13 11:30
 */
@Slf4j(topic = "ExceptionHandler.class")
@RestControllerAdvice
public class MyExceptionHandler {

//    @ExceptionHandler(Exception.class)
//    public CommonResult handleException(Exception e) {
//        return CommonResult.failed("系统繁忙,请稍后再试");
//    }

    /**
     * ValidationException
     */
    @ExceptionHandler(ValidationException.class)
    public CommonResult handleValidationException(ValidationException e) {
        return CommonResult.failed(e.getCause().getMessage());
    }

    /**
     * 方法参数校验
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public CommonResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        return CommonResult.failed(e.getBindingResult().getFieldError().getDefaultMessage());
    }
}
